<template>
  <div class="content-div">
    <div class="page-title">今天想要找什么？</div>
    <van-search class="search-box" shape="round" v-model="searchKey" placeholder="请输入搜索内容" @search="searchInfo" />
    <van-tabs class="tab-box" color="#1989fa" line-width="20px" v-model="active">
      <van-tab title="服务" name="1"></van-tab>
      <van-tab title="医院" name="2"></van-tab>
      <van-tab title="陪诊师" name="3"></van-tab>
    </van-tabs>
    <div class="record-box">
      <div class="record-head">
        <span>历史搜索记录</span>
        <div class="clear-btn-box" @click="clearRecord">
          <span>清除</span>
          <van-icon name="cross" />
        </div>
      </div>
      <div v-if="recordList.length" class="record-list-box">
        <span @click="searchByKey(item)" v-for="(item, index) in recordList" :key="index">{{item}}</span>
      </div>
      <div v-else class="no-data-text">暂无搜索记录!</div>
    </div>
  </div>
</template>
<script>
import { Dialog } from 'vant'
// import { findScreenList } from '@/api/medical.js'
export default {
  name: 'index',
  data () {
    return {
      active: 1,
      searchKey: '',
      recordList: []
    }
  },
  created () {},
  methods: {
    searchByKey (key) {
      this.searchKey = key
      this.searchInfo()
    },
    searchInfo () {
      let list = this.searchKey ? this.recordList.includes(this.searchKey) ? this.recordList : [...this.recordList, this.searchKey] : this.recordList
      localStorage.setItem('PZ_SEARCH_RECORD', JSON.stringify(list))
      this.$router.push({
        path: '/searchList',
        query: {
          searchKey: this.searchKey,
          type: this.active
        }
      })
    },
    clearRecord () {
      Dialog.confirm({
        title: '提示',
        message: '确认清除历史搜索记录？',
        confirmButtonColor: '#1989fa'
      }).then(() => {
        this.recordList = []
        localStorage.removeItem('PZ_SEARCH_RECORD')
      }).catch(() => {
        // on cancel
      })
    }
  },
  mounted () {
    this.recordList = localStorage.getItem('PZ_SEARCH_RECORD') ? JSON.parse(localStorage.getItem('PZ_SEARCH_RECORD')) : []
  }
}
</script>

<style scoped lang="less">
  .content-div{
    font-size: 12px;
    background-color: #FFFFFF;
    min-height: 100%;
    .page-title{
      font-size: 14px;
      padding: 14px 0 0 10px;
      font-weight: bold;
    }
    .search-box{
      padding: 10px;
      /deep/ .van-search__content{
        border: 1px solid #1989fa;
        background-color: #FFFFFF;
      }
      /deep/ .van-icon-search:before{
        color: #1989fa;
      }
    }
    /deep/ .van-tab{
      font-size: 12px;
    }
    /deep/ .van-tabs--line .van-tabs__wrap{
      height: 30px;
    }
    .tab-box{
      padding-bottom: 14px;
    }
    .record-box{
      width: 300px;
      margin-left: 10px;
      background-color: #F8F8F8;
      border-radius: 6px;
      padding: 10px;
      box-sizing: border-box;
      .record-head{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 10px;
        .clear-btn-box{
          color: #1989fa;
          border: 1px solid #1989fa;
          height: 24px;
          line-height: 24px;
          padding: 0 6px;
          border-radius: 12px;
          span{
            padding-right: 4px;
          }
        }
      }
      .record-list-box{
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: flex-start;
        flex-wrap: wrap;
        span{
          background-color: #FFFFFF;
          height: 24px;
          border-radius: 12px;
          padding: 0 10px;
          line-height: 24px;
          margin: 0 10px 10px 0;
          text-align: center;
        }
      }
      .no-data-text{
        text-align: center;
        padding: 10px 0;
      }
    }
  }
</style>
